#include "server.h"
#include "mysqlDBConnPool.h"

int main(int argv, char** argc)
{
  //std::string s = "main";
  //COM::logger = new qLogger(s);
  //COM::logger->addLocalFileLog("/home/wy/Game/server/log/server.log");

  //while(1)
  //{
  //LOG_ERROR("FFL[%s, %s, %d]", LOG_FFL);
  //LOG_INFO("你好 FFL[%s,%s,%d]", LOG_FFL);
  //LOG_WARN("你好");
  //LOG_DEBUG("你好");
  //sleep(10);
  //}

  //LOG_INFO("%u, %s", __LINE__, __func__);
  //LOG_INFO("%s,%s,%d", LOG_FFL);
  //LOG_INFO("begin");
  Server* s = new Server;
  if(!s->init())
  {
    return -1;
  }
  s->start();
  return 0;
}

int tet(int argv, char** argc)
{
//  if(argv < 2)
//  {
//    std::cerr << "please enter port" << std::endl;
//    return 0;
//  }
//
//  size_t port = atoi(argc[1]);
  //Server* s = new Server;
  //s->init();
  //s->start();
  

  MysqlConnPool* p = MysqlConnPool::getInstance(); 
  p->putUrl(0, "mysql://root:Root_123@127.0.0.1:3306/testdb");

  Connid connid = p->getHandle(0);
  //const char* sql = "CREATE TABLE TESTTABLE(ACCOUNT VARCHAR(10), PASSWD VARCHAR(10))";
  //if(p->exeCreate(connid, sql) == -1)
  //{
  //  LOG_ERROR("fail");
  //}
  db_col info[] =
  {
    {"ACCOUNT", DB_STR, sizeof(char)* 6},
    {"PASSWD", DB_STR, sizeof(char)*6},
    {NULL, 0, 0},
  };

  struct UserInfo
  {
    UserInfo()
    {
      bzero(this, sizeof(*this));
    }
    char account[6];
    char passwd[6];
  };
  UserInfo u;
  strcpy(u.account, "123");
  strcpy(u.passwd, "456");
  //if(-1 == p->exeInsert(connid, "TESTTABLE", info, (const unsigned char*)&u))
  //{
  //  LOG_ERROR("insert fail");
  //}

  UserInfo* u2 = new UserInfo[2];
  LOG_INFO("%p", u2);
  if(-1 == p->exeSelect(connid, "TESTTABLE", info, nullptr, nullptr, nullptr, (unsigned char**)&u2))
  {
    LOG_ERROR("insert fail");
    return -1;
  }
  for(int i = 0; i < 2; ++i)
  {
    std::cout << i << ":" << u2[i].account << ":" << u2[i].passwd << std::endl;
  }

  strcpy(u.account, "qwe");
  strcpy(u.passwd, "asd");
  char where[256] = {0};
  sprintf(where, "ACCOUNT=%d", 123);
  LOG_INFO(where);
  p->exeUpdate(connid, "TESTTABLE", info, (const unsigned char*)&u, where);

  UserInfo* u3 = new UserInfo[2];
  if(-1 == p->exeSelect(connid, "TESTTABLE", info, nullptr, nullptr, nullptr, (unsigned char**)&u3))
  {
    LOG_ERROR("insert fail");
  }
  for(int i = 0; i < 2; ++i)
  {
    std::cout << i << ":" << u3[i].account << ":" << u3[i].passwd << std::endl;
  }

  memset(where, 0, sizeof(where));
  sprintf(where, "ACCOUNT=\'%s\'", "qwe");
  LOG_INFO(where);
  p->exeDelete(connid, "TESTTABLE", where); 

  return 0;
}
